package com.lsk.netdisk.commons.authz.utils;

import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class AOPUtil {
	public static <T> T getParam(ProceedingJoinPoint pjp, String name, Class<T> type) throws NoSuchMethodException {
		Object[] args = pjp.getArgs();
		Class<?>[] argTypes = new Class[args.length];
		for (int i = 0;i < args.length;i++){
			argTypes[i] = args[i].getClass();
		}
		String methodName = pjp.getSignature().getName();
		Class<?> targetClass = pjp.getSignature().getDeclaringType();
		Method method = targetClass.getDeclaredMethod(methodName,argTypes);
		Parameter[] parameters = method.getParameters();
		int parameterIndex = 0;
		for (int i = 0;i < parameters.length;i++){
			if(parameters[i].getName().equals(name)){
				parameterIndex = i;
			}
		}
		return (T) args[parameterIndex];
	}
}
